/**
 * Copyright 2008 Vidar Svansson
 *
 * This file is part of JAdapter.
 *
 * JAdapter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JAdapter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JAdapter.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jadapter.spring.config;

import org.jadapter.spring.SpringAdapter;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.beans.factory.parsing.Location;
import org.springframework.beans.factory.parsing.Problem;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

/**
 *
 * @author Vidar Svansson
 * @since 0.3
 */
public class AdapterBeanDefinitionParser
        extends AbstractBeanDefinitionParser {

    protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {

        BeanDefinitionBuilder bean = BeanDefinitionBuilder.genericBeanDefinition(SpringAdapter.class);

        String pattern = element.getAttribute("class");

        try {
            Class factory = Class.forName(pattern);
            bean.addConstructorArgValue(factory);
            parseBean(element, parserContext, pattern, factory);

        } catch (ClassNotFoundException e) {

            throw new BeanDefinitionParsingException(
                    new Problem("adapter class not found " + pattern,
                        new Location(parserContext.getReaderContext().getResource()))
            );
        }



        bean.addConstructorArgValue(pattern);

        String transformer = element.getAttribute("transformer");

        if(transformer != null)
            bean.addConstructorArgValue(transformer);

        String to = element.getAttribute("to");
        if (StringUtils.hasText(to)) {
            bean.addPropertyValue("to", to);
        }
        String from = element.getAttribute("from");
        if (StringUtils.hasText(to)) {
            bean.addPropertyValue("from", from);
        }


        AbstractBeanDefinition def =  bean.getBeanDefinition();


        return def;
    }

    private void parseBean(Element element, ParserContext parserContext, String beanName, Class factory) {
        BeanDefinitionBuilder factoryBeanBuilder = BeanDefinitionBuilder.genericBeanDefinition(factory);

        BeanDefinition factoryBean = factoryBeanBuilder.getBeanDefinition();

        // TODO parse more?
        parserContext.getDelegate().parsePropertyElements(element, factoryBean);
        parserContext.getDelegate().parseConstructorArgElements(element, factoryBean);

        // TODO make optional?
        factoryBean.setScope("prototype");


        parserContext.getRegistry().registerBeanDefinition(beanName, factoryBean);
    }

}
